<template>
  <div class="app-container">
    <!--filter-->
    <el-form :inline="true" :model="filters" ref="ruleForm" class="demo-form-inline">
      <el-form-item label="查询时间" prop="time">
        <el-date-picker
          v-model="form.time"
          type="datetimerange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerDayStep">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="操作人类型" prop="orderType">
        <el-select
          v-model="filters.optPeopleType" width="100%" placeholder="请选择操作人类型">
          <el-option :value="-1" label="全部"></el-option>
          <el-option :value="1" label="公司"></el-option>
          <el-option :value="2" label="上级代理"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作人" prop="orderType">
        <el-select
          v-model="filters.parentId" width="100%" placeholder="请选择操作人">
          <el-option
            :label="'全部'"
            :key="consts.ALL"
            :value="consts.ALL"
          ></el-option>
          <el-option
            v-for="(item,index) in operatorList"
            v-if="item"
            :label="item.optUserName"
            :key="index"
            :value="item.optUserId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
      <el-button v-if="roleCheck('export-set-record')" class="filter-item" type="primary" icon="el-icon-download" @click="exportHandle">导出</el-button>
      <el-button @click="cancel('ruleForm')">重置</el-button>
    </el-form>
    <!--table-->
    <el-table
      class="el-table-align-center"
      :data="tableData.list"
      border
      v-loading="loading">
      <el-table-column label="授信设置时间">
        <template slot-scope="{row}">
          {{row.createTime | toTime }}
        </template>
      </el-table-column>
      <el-table-column label="设置前的账户余额" prop="creditedAmountBefore">
        <template slot-scope="{row}">
          {{row.creditedAmountBefore | currency}}
        </template>
      </el-table-column>
      <el-table-column label="设置后的账户余额" prop="creditedAmountAfter">
        <template slot-scope="{row}">
          {{row.creditedAmountAfter | currency}}
        </template>
      </el-table-column>
      <el-table-column label="操作人类型">
        <template slot-scope="{row}">
          {{filterPeopleType(row.optPeopleType)}}
        </template>
      </el-table-column>
      <el-table-column label="操作人">
        <template slot-scope="{row}">
          {{row.optUserName}}
        </template>
      </el-table-column>
    </el-table>
    <!--page-->
    <div class="pagination-container">
      <el-pagination background @size-change="gridSizeChange" @current-change="gridCurrentChange"
                     :current-page="filters.page" :page-sizes="gridPageSize" :page-size="filters.size"
                     :layout="gridLayout" :total="tableData.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getOperatorList } from '@/services/api/audit';
import { getMemberAdjustList, exportMembersAllRecordList } from '@/services/api/member';
import { grid } from '@/build';
import { form, pickerOption } from '@/build';

export default {
  name: 'my-address',
  mixins: [form, grid, pickerOption],

  data() {
    return {
      localSummariesText: '当前页汇总 / 所有页汇总',
      tableData: {},
      operatorList: [],
      filters: {
        proxyId: this.$store.state.member.member_id,
        optPeopleType: -1,
        optType: 3,
        parentId: undefined,
        page: 1,
        size: 20,
      },
      form: {
        time: [moment().startOf('month').format('YYYY-MM-DD 00:00:00'), moment().format('YYYY-MM-DD 23:59:59')],
      },
      filterDateType: false,
      statistic: [
        {
          index: 1,
          prop: 'creditedAmountBefore',
          format: (value) => <div> {_.currency(value)} / {_.currency(this.tableData.creditBeforeCount)} </div>,
        },
        {
          index: 2,
          prop: 'creditedAmountAfter',
          format: (value) => <div> {_.currency(value)} /  {_.currency(this.tableData.creditAfterCount)}</div>,
        },
      ],
    };
  },
  methods: {
    cancel(formName) {
      this.resetForm(formName);
      this.form = {
        time: [moment().startOf('month').format('YYYY-MM-DD 00:00:00'), moment().format('YYYY-MM-DD 23:59:59')],
      },
      this.submitFilterForm({
        proxyId: this.$store.state.member.member_id,
        optPeopleType: -1,
        optType: 3,
        parentId: undefined,
        page: 1,
        size: 20,
      });
    },
    filterPeopleType(type) {
      switch (type) {
        case 1:
          return '公司';
        case 2:
          return '上级代理';
        default:
          return '';
      }
    },
    // 导出
    exportHandle() {
      const { 
proxyId, optPeopleType, parentId, optType 
} = this.filters;
      const maintainTime = this.form.time;
      let startDate; let 
endDate;
      if (!_.isUndefined(maintainTime) && _.isArray(maintainTime)) {
        startDate = maintainTime[0];
        endDate = maintainTime[1];
      }
      const ajaxData = { 
proxyId, startDate, endDate, optPeopleType, parentId, optType
 };
      const filename = '上分设置记录.csv';
      const header = ['上分设置时间', '设置前的上分金额', '设置后的上分金额', '操作人类型', '操作人'];
      const filterField = ['createTime', 'creditedAmountBefore', 'creditedAmountAfter', 'optPeopleType', 'optUserName'];
      exportMembersAllRecordList(ajaxData).then(([result, data]) => {
        if (result) {
          console.log(result);
          console.log(data);
          const exportData = this.responseFormatter(data.list);
          this.export2Excel(header, filterField, exportData, filename);
        } else {
          this.$message({ message: `导出失败,${  data.message}`, type: 'warning' });
        }
      });
    },
    responseFormatter(data) {
      const endData = [];
      data.forEach((item) => {
        endData.push({
          createTime: _.toTime(item.createTime),
          creditedAmountBefore: _.currency(item.creditedAmountBefore),
          creditedAmountAfter: _.currency(item.creditedAmountAfter),
          optPeopleType: this.filterPeopleType(item.optPeopleType),
          optUserName: item.optUserName,
        });
      });
      return endData;
    },
  },

  mounted() {
    this.bindGet(getOperatorList, 'operatorList', {
      sendingData: { proxyId: this.filters.proxyId },
    });
    this.bindGet(getMemberAdjustList, 'tableData', {
      action: 'getGrid',
      sendingData: 'filters',
      beforeSend: (data) => {
        const maintainTime = this.form.time;
        let startDate; let 
endDate;
        if (!_.isUndefined(maintainTime) && _.isArray(maintainTime)) {
          startDate = maintainTime[0];
          endDate = maintainTime[1];
          data = { ...this.filters, startDate, endDate };
        } else {
          data.startDate = undefined;
          data.endDate = undefined;
        }
        return data;
      },
    });
    this.getAll();
  },
};
</script>
